{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法一：暴力破解，时间O(2^(n1+n2))\n",
    "import functools\n",
    "class Solution:\n",
    "    def isInterleave(self, s1: str, s2: str, s3: str) -> bool:\n",
    "        n1, n2, n3 = len(s1), len(s2), len(s3)\n",
    "        if(n1 + n2 != n3):\n",
    "            return False\n",
    "        @functools.lru_cache(None) # 不用会超时\n",
    "        def Judge(pos1, pos2, pos3):\n",
    "            if(pos3 == n3):\n",
    "                return True\n",
    "            res = False\n",
    "            if(pos1 < n1 and s1[pos1] == s3[pos3]):\n",
    "                res = res or Judge(pos1+1, pos2, pos3+1)\n",
    "            if(res == False and pos2 < n2 and s2[pos2] == s3[pos3]):\n",
    "                res = res or Judge(pos1, pos2+1, pos3+1)\n",
    "            return res\n",
    "        return Judge(0,0,0)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法二：动态规划（二维）\n",
    "#动态方程:\n",
    "'''\n",
    "dp[i][j] = (dp[i-1][j] && s3[i+j-1] == s1[i-1]) || (dp[i][j-1] && s2[j-1] == s3[i+j-1])\n",
    "'''\n",
    "\n",
    "class Solution:\n",
    "    def isInterleave(self, s1: str, s2: str, s3: str) -> bool:\n",
    "        n1, n2, n3 = len(s1), len(s2), len(s3)\n",
    "        if(n1+n2 != n3):\n",
    "            return False\n",
    "        dp = [[False]*(n1+1) for r in range(n2+1)]\n",
    "        dp[0][0] = True\n",
    "        rows = n2 + 1\n",
    "        cols = n1 + 1\n",
    "        for c in range(1, cols):\n",
    "            dp[0][c] = dp[0][c-1] and s1[c-1] == s3[c-1]\n",
    "        for r in range(1, rows):\n",
    "            dp[r][0] = dp[r-1][0] and s2[r-1] == s3[r-1]\n",
    "        for r in range(1, rows):\n",
    "            for c in range(1, cols):\n",
    "                dp[r][c] = (dp[r][c-1] and s1[c-1] == s3[r+c-1]) or (dp[r-1][c] and s2[r-1] == s3[r+c-1])\n",
    "        return dp[-1][-1]\n",
    "        \n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "a = Solution()\n",
    "s1 = \"aabcc\"\n",
    "s2 = \"dbbca\"\n",
    "s3 = \"aadbbcbcac\"\n",
    "#s1 = \"aabcc\"\n",
    "#s2 = \"dbbca\"\n",
    "#s3 = \"aadbbbaccc\"\n",
    "s1 = \"bbbbbabbbbabaababaaaabbababbaaabbabbaaabaaaaababbbababbbbbabbbbababbabaabababbbaabababababbbaaababaa\"\n",
    "s2 = \"babaaaabbababbbabbbbaabaabbaabbbbaabaaabaababaaaabaaabbaaabaaaabaabaabbbbbbbbbbbabaaabbababbabbabaab\"\n",
    "s3 = \"babbbabbbaaabbababbbbababaabbabaabaaabbbbabbbaaabbbaaaaabbbbaabbaaabababbaaaaaabababbababaababbababbbababbbbaaaabaabbabbaaaaabbabbaaaabbbaabaaabaababaababbaaabbbbbabbbbaabbabaabbbbabaaabbababbabbabbab\"\n",
    "  \n",
    "print(a.isInterleave(s1,s2,s3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Example 1:\n",
    "\n",
    "Input: s1 = \"aabcc\", s2 = \"dbbca\", s3 = \"aadbbcbcac\"\n",
    "Output: true\n",
    "Example 2:\n",
    "\n",
    "Input: s1 = \"aabcc\", s2 = \"dbbca\", s3 = \"aadbbbaccc\"\n",
    "Output: false\n",
    "\n",
    "s1 = \"bbbbbabbbbabaababaaaabbababbaaabbabbaaabaaaaababbbababbbbbabbbbababbabaabababbbaabababababbbaaababaa\"\n",
    "s2 = \"babaaaabbababbbabbbbaabaabbaabbbbaabaaabaababaaaabaaabbaaabaaaabaabaabbbbbbbbbbbabaaabbababbabbabaab\"\n",
    "s3 = \"babbbabbbaaabbababbbbababaabbabaabaaabbbbabbbaaabbbaaaaabbbbaabbaaabababbaaaaaabababbababaababbababbbababbbbaaaabaabbabbaaaaabbabbaaaabbbaabaaabaababaababbaaabbbbbabbbbaabbabaabbbbabaaabbababbabbabbab\"\n",
    "    \n",
    "来源：力扣（LeetCode）\n",
    "链接：https://leetcode-cn.com/problems/interleaving-string\n",
    "著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
